//
//  MainMenuController.m
//  MathMasterMain
//
//  Created by Jimmy Hahm on 10-03-13.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MainMenuController.h"
#import "ProfileXMLWriter.h"
#import "ProfileMenuController.h"
#import	"SwitchViewController.h"
#import "ProfileCreationController.h"
#import "ProfileXMLWriter.h"
#import	"ProfileXMLParser.h"


@implementation MainMenuController


@synthesize profileMenuController;
@synthesize Profile;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

-(IBAction)setTitleNameDisplay:(id)sender{
	NSString *name = [sender currentTitle];
	[nameDisplay setText:name];
	[nameDisplay setTextColor:[UIColor blackColor]];
	
}
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

- (void)viewDidLoad {
	[self reloadProfile];
	[super viewDidLoad];
}

-(void)reloadProfile
{
	ProfileXMLWriter *PXML = [ProfileXMLWriter new];
	[PXML startProfile];
	
	ProfileXMLParser* xmlParser = [ ProfileXMLParser new ];
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"PROFILE.xml"];
	NSMutableArray* Q = [ xmlParser parseContentOfpath:appFile ];
	NSString *temp1 = [[Q objectAtIndex:0] name];
	
	if(temp1 != nil)
	{
		[profile1 setTitle:temp1 forState:UIControlStateNormal];
	}
	else {
		[profile1 setTitle:@"Empty" forState:UIControlStateNormal];

	}

	
	NSString *temp2 = [[Q objectAtIndex:1] name];
	if(temp2 != nil)
	{
		[profile2 setTitle:temp2 forState:UIControlStateNormal];
	}else {
		[profile2 setTitle:@"Empty" forState:UIControlStateNormal];
		
	}
	
	
	NSString *temp3 = [[Q objectAtIndex:2] name];
	if(temp3 != nil)
	{
		[profile3 setTitle:temp3 forState:UIControlStateNormal];
	}else {
		[profile3 setTitle:@"Empty" forState:UIControlStateNormal];
		
	}
	
	
	NSString *temp4 = [[Q objectAtIndex:3] name];
	if(temp4 != nil)
	{
		[profile4 setTitle:temp4 forState:UIControlStateNormal];
	}else {
		[profile4 setTitle:@"Empty" forState:UIControlStateNormal];
		
	}
	
	
	[PXML release];
	[xmlParser release];
	[Q removeAllObjects];
	[temp1 release];
	[temp2 release];
	[temp3 release];
	[temp4 release];
}


- (void)actionSheet:(UIActionSheet *)actionSheet
clickedButtonAtIndex:(NSInteger)buttonIndex {
	
    if (buttonIndex == 0) {
		ProfileXMLWriter *s = [ProfileXMLWriter new];
		[s deleteProfile:[nameDisplay text]];
		
		[self reloadProfile];
		[self.Profile setNeedsDisplay];
		[self.view addSubview:self.Profile];
		[self.view bringSubviewToFront:self.Profile];
		
	} else if (buttonIndex == 1) {}
	
}

-(IBAction)deleteThisProfile{
	NSString *msgAlert = @"Do You Really Want to Delete This Profile?";
	UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:msgAlert delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:@"Yes" otherButtonTitles:nil];
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
	[actionSheet showInView:self.view];
	[actionSheet release];
	
}

-(IBAction)openCreateMenu:(id)sender
{

	ProfileCreationController *X = [ProfileCreationController alloc];
	[self presentModalViewController:X animated:YES];
	
	[X release];
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
